package com.example.sdk_test_demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mchsdk.open.MCApiFactory;


/**
 * 这个Activity用于设置测试参数，仅测试使用。
 */
public class TestActivity extends Activity{

	private final String TAG = "MainActivity";

	EditText gameId;
	EditText gameName;
	EditText gameAppid;
	EditText paysdk_signkey;
	EditText goodsPrice;
	EditText ipAddress;
	Button setGameParams;

//	final static String l1 = "http://a2.vlcms.com/sdk.php";
//	final static String l2 = "hk.php";
//	final static String l3 = "h.php";
//	final static String l4 = "hphp";
//	final static String l5 = "h";//
//	final static String l6 = "h.php";// 暴走
//	final static String l7 = "http://a2.vlcms.com/sdk.php";//溪谷手游
//			//+ "http://sdk.3011.cn";// 九毛科技
//	final static String l8 = "h0/sdk.php";// 道蒙
//	final static String l9 = "hdk.php";// 台湾米洛
//	final static String l10 = "umao/sdk.php";
//	final static String l11 = "k.php";
//	final static String l12 = "wSY/sdk.php";
//	final static String l13 = ".php";
//	final static String l14 = "k.php";
//	final static String l15 = "umao/sdk.php";
//	final static String l16 = "wSY/sdk.php";// yyh
//	final static String l17 = "wSY/sdk.php"; // 王贺本地地址
//	final static String l18 = "wan/sdk.php";// 王贺本地——变态玩
//	final static String l19 = "";
//	final static String l20 = "sdk.php";
//	private static final String[] autoStr = new String[] { l1, l2, l3, l4, l5,
//			l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, l17, l18, l19 };

//	static {
//		System.loadLibrary("checksignkey");
//	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_params);
		initView();
		initData();
	}

	private void initView() {
		gameName = (EditText) findViewById(R.id.edt_gameName);
		gameAppid = (EditText) findViewById(R.id.edt_gameAppId);
		gameId = (EditText) findViewById(R.id.edt_gameId);
		paysdk_signkey = (EditText) findViewById(R.id.edt_paysdk_signkey);
		ipAddress = (EditText) findViewById(R.id.edt_paysdk_ipaddress);

		goodsPrice = (EditText) findViewById(R.id.edt_goodsprice);

		setGameParams = (Button) findViewById(R.id.btn_setgameparams);
		setGameParams.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setTestGameParames();
			}
		});

	}

	private void initData(){
		gameName.setText(MCApiFactory.getMCApi().getMap().get("paysdk_gamename"));
		gameAppid.setText(MCApiFactory.getMCApi().getMap().get("paysdk_gameappid"));
		gameId.setText(MCApiFactory.getMCApi().getMap().get("paysdk_gameid"));
		paysdk_signkey.setText(MCApiFactory.getMCApi().getMap().get("paysdk_signkey"));
		ipAddress.setText(MCApiFactory.getMCApi().getMap().get("paysdk_address"));

	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	private void setTestGameParames(){
		String gameNameStr = gameName.getText().toString().trim();
		String gameAppIdStr = gameAppid.getText().toString().trim();
		String gameIdStr = gameId.getText().toString().trim();
		String gamePrice = goodsPrice.getText().toString().trim();
		String gameSignkey = paysdk_signkey.getText().toString().trim();
		String gameIP = ipAddress.getText().toString().trim();
		if(TextUtils.isEmpty(gameNameStr) ||
				TextUtils.isEmpty(gameAppIdStr) ||
				TextUtils.isEmpty(gameIdStr) ||
				TextUtils.isEmpty(gameSignkey) ||
				TextUtils.isEmpty(gameIP)){
			Toast.makeText(TestActivity.this, "请输入游戏参数", Toast.LENGTH_LONG)
					.show();
			return;
		}

//		checkSignKey(gameSignkey);

		int price = 1;
		if(!TextUtils.isEmpty(gamePrice)){
			try {
				price = Integer.valueOf(gamePrice);
			}catch (NumberFormatException e){
				price = 1;
			}
		}

		if(price > 999999){
			Toast.makeText(TestActivity.this, "商品价格太大！", Toast.LENGTH_LONG).show();
		}else {
			MCApiFactory.getMCApi().setParams("0", "自然注册",
					gameIdStr, gameNameStr, gameAppIdStr, gameSignkey, gameIP);
			MCApiFactory.getMCApi().init(this);

			Toast.makeText(TestActivity.this, "保存成功", Toast.LENGTH_LONG)
					.show();

			Intent intent = new Intent();
			intent.putExtra("price", price);
			setResult(1, intent);
			finish();
		}
	}

	private void checkSignKey(String str){
		byte[] res = Base64.decode(str, Base64.DEFAULT);
		Log.w(TAG, "SignKey:" + new String(res));

//		Log.w(TAG, "SignKey:" + stringFromJNI(str));
	}

//	public native String stringFromJNI(String secStr);

}
